﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;
using Microsoft.Win32;
using Microsoft.Office.Core;

namespace olkbutton4
{
    public partial class OlkButtonRibbon
    {
        private OlkConfig.OlkButton4Config config;

        private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
        {
            OlkConfig olkConfig = new OlkConfig();
            this.config = olkConfig.GetConfig();
            if (!config.showConfiButton) confiToggle.Visible = false;
            if (!config.showPrivateButton) privateToggle.Visible = false;
            if (!config.showPersonalButton) personalToggle.Visible = false;
            if (!config.showNoneButton) noneToggle.Visible = false;
            // set default sensitivity according to configurtion
            if (config.defaultSensitivity != Outlook.OlSensitivity.olNormal && GetSensitivity() == Outlook.OlSensitivity.olNormal)
            {
                ToggleSensitivity(config.defaultSensitivity);
                UpdateSubject();
            }
            UpdateButtons();
            
        }

        public Outlook.MailItem GetCurrentMailItem()
        {
            Outlook.Application application = Globals.OlkButton4.Application;
            Outlook.Inspector inspector = application.ActiveInspector();
            Outlook.MailItem item = inspector.CurrentItem as Outlook.MailItem;
            if (item != null)
            {
                return item;
            }
            return null;
        }

        private void UpdateSubject()
        {
            if (config.addSubjectConfidential && IsSensitivity(Outlook.OlSensitivity.olConfidential))
            {
                AddToSubject(config.subjectStringConfidential);
            }
            else if (config.addSubjectPersonal && IsSensitivity(Outlook.OlSensitivity.olPersonal))
            {
                AddToSubject(config.subjectStringPersonal);
            }
            else if (config.addSubjectPrivate && IsSensitivity(Outlook.OlSensitivity.olPrivate))
            {
                AddToSubject(config.subjectStringPrivate);
            }
        }

        private void AddToSubject(string stringToAdd)
        {
            Outlook.MailItem myItem = GetCurrentMailItem();
            if (myItem != null)
            {
                string oldSubject = myItem.Subject;
                if (oldSubject != null)
                {
                    // check if the string is already in there
                    if (!oldSubject.Contains(stringToAdd))
                    {
                        myItem.Subject = stringToAdd + " " + oldSubject;
                    }
                }
                else
                {
                    myItem.Subject = stringToAdd + " ";
                }
            }
        }

        private void ClickSend()
        {
            // this does not work ....
            Outlook.MailItem myItem = GetCurrentMailItem();
            if (myItem != null)
            {
                CommandBarButton sendBtn;
                sendBtn = myItem.GetInspector.CommandBars.FindControl(MsoControlType.msoControlButton, 3037, Type.Missing, Type.Missing) as CommandBarButton;
                if (sendBtn == null)
                {
                    sendBtn = myItem.GetInspector.CommandBars.FindControl(MsoControlType.msoControlButton, 2617, Type.Missing, Type.Missing) as CommandBarButton;
                }

                if (sendBtn != null)
                {
                    sendBtn.Execute();
                }
            }
        }

        private void UpdateButtons()
        {
            Outlook.MailItem myItem = GetCurrentMailItem();
            if (myItem != null)
            {
                switch (myItem.Sensitivity)
                {
                    case Outlook.OlSensitivity.olConfidential:
                        this.confiToggle.Checked = true;
                        this.personalToggle.Checked = false;
                        this.privateToggle.Checked = false;
                        break;
                    case Outlook.OlSensitivity.olPersonal:
                        this.confiToggle.Checked = false;
                        this.personalToggle.Checked = true;
                        this.privateToggle.Checked = false;
                        break;
                    case Outlook.OlSensitivity.olPrivate:
                        this.confiToggle.Checked = false;
                        this.personalToggle.Checked = false;
                        this.privateToggle.Checked = true;
                        break;
                    default:
                        break;
                }
            }
        }

        private void ToggleSensitivity(Outlook.OlSensitivity targetSensitivity)
        {
            Outlook.MailItem myItem = GetCurrentMailItem();
            if (myItem != null)
            {
                if (myItem.Sensitivity == targetSensitivity)
                {
                    myItem.Sensitivity = Outlook.OlSensitivity.olNormal;
                }
                else
                {
                    myItem.Sensitivity = targetSensitivity;
                }

            }
        }

        private Outlook.OlSensitivity  GetSensitivity()
        {
            Outlook.MailItem myItem = GetCurrentMailItem();
            if (myItem != null)
            {
                return myItem.Sensitivity;
            }
            return Outlook.OlSensitivity.olNormal;
        }

        private bool IsSensitivity(Outlook.OlSensitivity match)
        {
            return (GetSensitivity() == match);
        }

        private void confiToggle_Click(object sender, RibbonControlEventArgs e)
        {
            ToggleSensitivity(Outlook.OlSensitivity.olConfidential);
            UpdateSubject();
            UpdateButtons();
        }

        private void noneToggle_Click(object sender, RibbonControlEventArgs e)
        {
            ToggleSensitivity(Outlook.OlSensitivity.olNormal);
            UpdateSubject();
            UpdateButtons();
        }

        private void privateToggle_Click(object sender, RibbonControlEventArgs e)
        {
            ToggleSensitivity(Outlook.OlSensitivity.olPrivate);
            UpdateSubject();
            UpdateButtons();
        }

        private void personalToggle_Click(object sender, RibbonControlEventArgs e)
        {
            ToggleSensitivity(Outlook.OlSensitivity.olPersonal);
            UpdateSubject();
            UpdateButtons();
        }

        private void ShowErrorMessage(string message)
        {
                MessageBox.Show(message,
                        "ERROR"
                        , MessageBoxButtons.OK
                        , MessageBoxIcon.Error);
        }

    }
}
